import 'package:built_collection/built_collection.dart';
import 'package:equatable/equatable.dart';
import 'package:mobile_app.core/core/domain/entities/learning_content/lexical_item/lexical_item.dart';
import 'package:mobile_app.core/core/domain/utils/uuid_generator.dart';
import 'package:mobile_app.home.core/core/domain/helpers/is_custom_lexical_item_title_valid/helper.dart';
import 'package:mobile_app.home.core/core/domain/helpers/is_custom_lexical_item_translation_valid/helper.dart';
import 'package:mobile_app.home.core/core/domain/helpers/is_custom_lexical_item_use_case_example_valid/helper.dart';
import 'package:mobile_app.home.core/core/domain/helpers/is_custom_lexical_item_use_case_translation_valid/helper.dart';
import 'package:mobile_app.home.core/custom_lexical_item_editing_page/domain/entities/custom_lexical_item_input.dart';
import 'package:mobile_app.home.core/custom_lexical_item_editing_page/domain/entities/custom_lexical_item_use_case_input.dart';

part 'result.dart';

abstract class Initialize {
  InitializationResult call({
    required String lexicalItemId,
    required BuiltMap<String, LexicalItem> lexicalItemMap,
  });
}

class InitializeImpl implements Initialize {
  const InitializeImpl({
    required IsCustomLexicalItemTitleValid isCustomLexicalItemTitleValid,
    required IsCustomLexicalItemTranslationValid isCustomLexicalItemTranslationValid,
    required IsCustomLexicalItemUseCaseExampleValid isCustomLexicalItemUseCaseExampleValid,
    required IsCustomLexicalItemUseCaseTranslationValid isCustomLexicalItemUseCaseTranslationValid,
  })  : _isCustomLexicalItemTitleValid = isCustomLexicalItemTitleValid,
        _isCustomLexicalItemTranslationValid = isCustomLexicalItemTranslationValid,
        _isCustomLexicalItemUseCaseExampleValid = isCustomLexicalItemUseCaseExampleValid,
        _isCustomLexicalItemUseCaseTranslationValid = isCustomLexicalItemUseCaseTranslationValid;

  final IsCustomLexicalItemTitleValid _isCustomLexicalItemTitleValid;
  final IsCustomLexicalItemTranslationValid _isCustomLexicalItemTranslationValid;
  final IsCustomLexicalItemUseCaseExampleValid _isCustomLexicalItemUseCaseExampleValid;
  final IsCustomLexicalItemUseCaseTranslationValid _isCustomLexicalItemUseCaseTranslationValid;

  @override
  InitializationResult call({
    required String lexicalItemId,
    required BuiltMap<String, LexicalItem> lexicalItemMap,
  }) {
    final lexicalItem = lexicalItemMap[lexicalItemId] as CustomLexicalItem;

    final lexicalItemTitleValid = _isCustomLexicalItemTitleValid(
      customLexicalItemTitle: lexicalItem.title,
    );

    final lexicalItemTranslationValid = _isCustomLexicalItemTranslationValid(
      customLexicalItemTranslation: lexicalItem.translation,
    );

    final useCaseInputs = lexicalItem.useCases.map((useCase) {
      final exampleValid = _isCustomLexicalItemUseCaseExampleValid(
        customLexicalItemUseCaseExample: useCase.exampleMarkdown,
      );

      final translationValid = _isCustomLexicalItemUseCaseTranslationValid(
        customLexicalItemUseCaseTranslation: useCase.translationMarkdown,
      );

      return CustomLexicalItemUseCaseInput(
        id: UuidGenerator().generateUuid(),
        example: useCase.exampleMarkdown,
        translation: useCase.translationMarkdown,
        exampleValid: exampleValid,
        translationValid: translationValid,
      );
    }).toBuiltList();

    final customLexicalItemInput = CustomLexicalItemInput(
      title: lexicalItem.title,
      translation: lexicalItem.translation,
      transcription: lexicalItem.transcription ?? '',
      titleValid: lexicalItemTitleValid,
      translationValid: lexicalItemTranslationValid,
      useCaseInputs: useCaseInputs,
    );

    final result = InitializationResult(
      customLexicalItemInput: customLexicalItemInput,
    );

    return result;
  }
}
